package com.yuanchu.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.store.dao.YoshopUserBalanceLogDao;
import com.yuanchu.store.dto.QueryUserParamsDto;
import com.yuanchu.store.entity.YoshopUserBalanceLogEntity;
import com.yuanchu.store.service.YoshopUserBalanceLogService;
import com.yuanchu.store.service.YoshopUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

import static com.yuanchu.common.utils.DateFormatUtils.convertStringToSeconds;


@Service("yoshopUserBalanceLogService")
public class YoshopUserBalanceLogServiceImpl extends ServiceImpl<YoshopUserBalanceLogDao, YoshopUserBalanceLogEntity> implements YoshopUserBalanceLogService {

    @Resource
    private YoshopUserService userService;

    @Override
    public PageResult<YoshopUserBalanceLogEntity> queryUserBalanceLog(Integer userId, Integer page) {
        // 全部
        if (page == null || page == 0){
            page = 1;
        }
        LambdaQueryWrapper<YoshopUserBalanceLogEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopUserBalanceLogEntity::getUserId, userId);
        lqw.orderByDesc(YoshopUserBalanceLogEntity::getCreateTime);
        Page<YoshopUserBalanceLogEntity> page1 = new Page<>(page, 15);
        Page<YoshopUserBalanceLogEntity> pageResult = this.page(page1, lqw);
        List<YoshopUserBalanceLogEntity> records = pageResult.getRecords();
        records.forEach(userLog -> userLog.setCreateTime(DateFormatUtils.formatDatetime(userLog.getCreateTime())));

        return PageResult.formatPageResult(records, page, 15, pageResult.getTotal());
    }

    @Override
    public boolean insertUserBalanceLog(Integer userId, Integer scene, String orderNo, String payPrice, Integer storeId) {
        YoshopUserBalanceLogEntity userBalanceLog = new YoshopUserBalanceLogEntity();
        userBalanceLog.setUserId(userId);
        // (10用户充值 20用户消费 30管理员操作 40订单退款)
        if (scene != null && scene == 20){
            userBalanceLog.setDescribe("用户消费："+ orderNo);
            userBalanceLog.setMoney("-" + payPrice);
        }
        userBalanceLog.setStoreId(storeId);
        userBalanceLog.setCreateTime(String.valueOf(Instant.now().toEpochMilli() / 1000));
        return this.save(userBalanceLog);
    }

    @Override
    public PageResult<YoshopUserBalanceLogEntity> queryUserPointsLog(QueryUserParamsDto param) {
        LambdaQueryWrapper<YoshopUserBalanceLogEntity> lqw = new LambdaQueryWrapper<>();
        String betweenTimeStart = param.getBetweenTimeStart();
        String betweenTimeEnd = param.getBetweenTimeEnd();
        if (StringUtils.isNoneEmpty(betweenTimeStart) && StringUtils.isNoneEmpty(betweenTimeEnd)){
            long start = convertStringToSeconds(betweenTimeStart);
            long end = convertStringToSeconds(betweenTimeEnd);
            lqw.between(YoshopUserBalanceLogEntity::getCreateTime, start,  end);
        }
        if(param.getScene() != null && param.getScene() > 0){
            lqw.eq(YoshopUserBalanceLogEntity::getScene, param.getScene());
        }
        lqw.orderByDesc(YoshopUserBalanceLogEntity::getCreateTime);
        Page<YoshopUserBalanceLogEntity> pageResult = this.page(new Page<>(param.getPage(), param.getPageSize()), lqw);
        List<YoshopUserBalanceLogEntity> records = pageResult.getRecords();
        records.forEach(userLog -> {
            userLog.setCreateTime(DateFormatUtils.formatDatetime(userLog.getCreateTime()));
            userLog.setUser(userService.queryUserByUserId(userLog.getUserId()));
        });
        if (param.getSearch() != null && !param.getSearch().isEmpty()){
            records = records.stream().filter(userLog -> userLog.getUser().getNickName().contains(param.getSearch())).collect(Collectors.toList());
        }

        return PageResult.formatPageResult(records, param.getPage(), param.getPageSize(), pageResult.getTotal());

    }


}